﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for CNews
/// </summary>
public class CNews
{
    public CNews()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public bool Add(string title, int parent, string datetime, string content)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Parent", parent);
        object obj = d.ExeScalar("select count(*) from news where parent=@Parent", pars);
        int sortcode = Convert.ToInt32(obj) + 1;
        pars.Add("Title", title);
        pars.Add("SortCode", sortcode);
        pars.Add("DateTime", datetime);
        pars.Add("Content", content);
        int c = d.ExeNonQuery("insert into news(parent,title,sortcode,[datetime],content) values(@Parent,@Title,@SortCode,@DateTime,@Content)", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public bool Delete(int id)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from news where id=@ID", pars);
        int c = 0;
        if (null != dt)
        {
            c = d.ExeNonQuery("delete from news where id=@ID", pars);
            pars.Clear();
            pars.Add("Parent", Convert.ToInt32(dt.Rows[0]["parent"].ToString()));
            pars.Add("SortCode", Convert.ToInt32(dt.Rows[0]["sortcode"].ToString()));
            c += d.ExeNonQuery("update news set sortcode=sortcode-1 where parent=@Parent and sortcode>@SortCode", pars);
        }
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public bool Update(int id, string title, string content, string datetime)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Title", title);
        pars.Add("Content", content);
        pars.Add("DateTime", datetime);
        pars.Add("ID", id);
        int c = d.ExeNonQuery("update news set title=@Title,content=@Content,[DateTime]=@DateTime where id=@ID", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public bool Sort(int id, bool isUp)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from news where id=@ID", pars);
        pars.Clear();
        pars.Add("Parent", Convert.ToInt32(dt.Rows[0]["parent"].ToString()));
        pars.Add("SortCode", Convert.ToInt32(dt.Rows[0]["sortcode"].ToString()));
        if (isUp)
        {
            dt = d.ExeDataTable("select top 1 * from news where  parent=@Parent and sortcode<@SortCode order by sortcode desc", pars);
        }
        else
        {
            dt = d.ExeDataTable("select top 1 * from news where  parent=@Parent and sortcode >@SortCode order by sortcode asc", pars);
        }
        pars.Clear();
        pars.Add("ID", id);
        int c = d.ExeNonQuery("update news set sortcode=" + (isUp ? "sortcode-1" : "sortcode+1") + " where id=@ID", pars);
        pars.Clear();
        if (null != dt)
        {
            pars.Add("ID", Convert.ToInt32(dt.Rows[0]["id"].ToString()));
            c += d.ExeNonQuery("update news set sortcode=" + (isUp ? "sortcode+1" : "sortcode-1") + " where id=@ID", pars);
        }
        d.Close();
        if (c > 0)
        {
            return true;
        }
        d.Close();
        return false;
    }

    public DataTable ReadNewses(int parent)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Parent", parent);
        DataTable dt = d.ExeDataTable("select * from news where parent=@Parent order by sortcode asc", pars);
        d.Close();
        return dt;
    }

    public DataRow ReadNews(int id)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from news where id=@ID", pars);
        d.Close();
        if (null != dt)
        {
            return dt.Rows[0];
        }
        return null;
    }
}